package com.example.firstproject.service;

import com.example.firstproject.entity.Course;
import com.example.firstproject.entity.XuanKe;
import com.example.firstproject.mapper.CourseMapper;
import com.example.firstproject.mapper.XuanKeMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class XuanKeService {

    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private XuanKeMapper xuanKeMapper;

    public Map<String,Object> xuanKe(String studentId,String courseId,int season){
        Course course = courseMapper.getById(courseId);
        Map<String ,Object> retMap = new HashMap<>();
        if(course == null){
            retMap.put("code",500);
            retMap.put("message","无此课程");
        }else {
            XuanKe xuanKe = xuanKeMapper.getByStuIdCouseId(studentId,courseId,season);
            if( xuanKe != null){
                retMap.put("code",500);
                retMap.put("message","学生已选课，请勿重复选课");
                log.debug( "xuanke info -------> {} ",xuanKe);
            }else {
                xuanKe = new XuanKe();
                xuanKe.setStudentId(studentId);
                xuanKe.setCourseId(courseId);
                xuanKe.setSeason(season);
                int result = xuanKeMapper.insert(xuanKe);
                if( result > 0 ){
                    retMap.put("code",200);
                    retMap.put("message","success");
                }else {
                    retMap.put("code",500);
                    retMap.put("message","error");
                }
            }
        }
        return retMap;
    }


}
